package javase2.day06.demo06;

public class RunnableImpl implements Runnable {
    //票源
    private int ticket=100;
    @Override
    public void run() {
        while (true){
            if (ticket>0){
                //为了提高出现安全问题的概率，让程序休眠一会
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"正在卖第"+ticket+"张票");
                ticket--;
            }
            //跳出死循环。之所以是<=0而不是=0是因为可能出现多线程问题ticket会<0
            if (ticket<=0){
                break;
            }
        }
    }
}
